package com.test.mapper;

import com.test.entity.User;
import com.test.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;

public class UserMapperTest {

    @Test
    public void testCRUD() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            // 使用不同的用户名，避免重复
            User newUser = new User("测试用户" + System.currentTimeMillis(), 22, "test@example.com");
            int insertRows = userMapper.insertUser(newUser);
            System.out.println("插入行数：" + insertRows);
            System.out.println("插入用户ID：" + newUser.getId());

            sqlSession.commit();

            // 查询用户列表
            List<User> userList = userMapper.queryUserList();
            System.out.println("查询到 " + userList.size() + " 条记录");
            for (User user : userList) {
                System.out.println("查询到的用户：" + user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}